//$(document).ready(function(){
//  $("div.login").css({
//	"position":"absolute",
//  	"left":"130px",
//  	"top":"120px"
//  });
//});

var data = window.lemon.js2client(5,null);
var isFirst = new String(data);
if(isFirst.search(/true/) != -1){
	window.lemon.js2client(201,"DROP TABLE User");
	window.lemon.js2client(201,"CREATE TABLE IF NOT EXISTS User (_id TEXT, phone_number TEXT);");
}else{
	var sqlrlt = window.lemon.searchTable("select _id from User;");
	var sqlrlt1 = new String(sqlrlt);
	var begin = sqlrlt1.lastIndexOf(":")+2;
	var end = sqlrlt1.lastIndexOf("\"");
	var user_id = sqlrlt1.substring(begin,end);
	window.lemon.js2client(2,"navigate.html?user_id="+user_id);
}

$(document).ready(function(){
	 $("div#div_login .login").fadeOut(0);
	$("div#div_login .cancel").fadeOut(0);
  	$("div#div_login .cancel").click(function(){
  	window.lemon.js2client(14,"");
  });
});

$(document).ready(function(){
	$("#nickname").click(function(){
		document.getElementById('nickname').value = '';
		 $("div#div_login .login").fadeIn(1000);
		  $("div#div_login .cancel").fadeIn(1000);
	});
});

var nick_name;
$(document).ready(function(){
	$("div#div_login .login").click(function(){
		var nick_name = document.getElementById('nickname').value;
		var n = new String(nick_name);
		if((n.length == 0) || (n.replace(/(^\s)|(\s*$)/g,"").length == 0)){
			window.lemon.js2client(7,"输个昵称再进入吧");
			return;
		}
		 $("div#div_login .login").fadeOut(0);
		$("div#div_login .cancel").fadeOut(0);
		var new_nick = new String(nick_name);
		new_nick = new_nick.replace(/( |[\s　])/g, ""); 
		var data = window.lemon.httpGet(server_address+"/tom/servlet/C2S0init?phone_number="+new_nick);
		var str = new String(data);
		if(str.search(/user_id/) == -1){
			window.lemon.js2client(7,"网络错误");
			
		}
		else{
			user_id = str.substring(str.lastIndexOf(':')+1,str.length);
			if(isFirst.search(/true/) != -1){
				var sql = "insert into User(_id,phone_number) values('"+user_id+"','"+new_nick+"');";
				window.lemon.js2client(201,sql);
				//window.lemon.js2client(6,null);
			}
			window.lemon.js2client(2,"navigate.html?user_id="+user_id);
		}
	});
});

